//-----------------------------------------------------------------------
// <copyright file="TerritoryDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public  class UnitSettingDAO
    {
		public  UnitSettingDAO()
		{
		}

		#region Common methods
		public virtual UnitSetting CreateUnitSettingFromReader(IDataReader reader)
		{
			UnitSetting item = new UnitSetting();
			try
			{
				if (!reader.IsDBNull(reader.GetOrdinal("UnitSettingID"))) item.UnitSettingID = (int)reader["UnitSettingID"];
				if (!reader.IsDBNull(reader.GetOrdinal("UnitCode"))) item.UnitCode = (string)reader["UnitCode"];
				if (!reader.IsDBNull(reader.GetOrdinal("CbmValue"))) item.CbmValue = (decimal)reader["CbmValue"];
				if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
				if (!reader.IsDBNull(reader.GetOrdinal("IsContainer"))) item.IsContainer = (bool)reader["IsContainer"];
				if (!reader.IsDBNull(reader.GetOrdinal("IsSystem"))) item.IsSystem = (bool)reader["IsSystem"];
				if (!reader.IsDBNull(reader.GetOrdinal("ConPointID"))) item.ConPointID = (int)reader["ConPointID"];
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at CreateUnitSettingFromReader method.");
			}
			return item;
		}
		#endregion
    
		#region CreateUnitSetting methods
        
		public virtual void CreateUnitSetting(UnitSetting unitSetting)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUnitSettingInsert");
	            
				dbCommandWrapper.AddInParameter("@UnitCode", DbType.String, unitSetting.UnitCode);
				dbCommandWrapper.AddInParameter("@CbmValue", DbType.Currency, unitSetting.CbmValue==Nulls.NullDecimal?DBNull.Value:(object)unitSetting.CbmValue);
				dbCommandWrapper.AddInParameter("@Description", DbType.String, unitSetting.Description);
	            
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at CreateUnitSetting method.");
			}
		}

		public virtual void CreateUnitSettingForConPoint(UnitSetting unitSetting)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUnitSettingInsertForConPoint");
	            
				dbCommandWrapper.AddInParameter("@UnitSettingID", DbType.String, unitSetting.UnitSettingID);
				dbCommandWrapper.AddInParameter("@ConPointID", DbType.String, unitSetting.ConPointID);
				dbCommandWrapper.AddInParameter("@CbmValue", DbType.Currency, unitSetting.CbmValue==Nulls.NullDecimal?DBNull.Value:(object)unitSetting.CbmValue);
				
	            
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at CreateUnitSettingForConPoint method.");
			}
		}
		#endregion

		#region UpdateUnitSetting methods
    
		public virtual void UpdateUnitSetting(UnitSetting unitSetting)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUnitSettingUpdate");            
            
				dbCommandWrapper.AddInParameter("@UnitSettingID", DbType.Int32, unitSetting.UnitSettingID);
				dbCommandWrapper.AddInParameter("@UnitCode", DbType.String, unitSetting.UnitCode);
				dbCommandWrapper.AddInParameter("@CbmValue", DbType.Currency, unitSetting.CbmValue==Nulls.NullDecimal?DBNull.Value:(object)unitSetting.CbmValue);
				dbCommandWrapper.AddInParameter("@Description", DbType.String, unitSetting.Description);
				dbCommandWrapper.AddInParameter("@IsContainer", DbType.Boolean, unitSetting.IsContainer);
				dbCommandWrapper.AddInParameter("@IsSystem", DbType.Boolean, unitSetting.IsSystem);
                            
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at UpdateUnitSetting method.");
			}
		}
    
		#endregion

		#region DeleteUnitSetting methods

		public virtual void DeleteUnitSetting(int  unitSettingID)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUnitSettingDelete");
	            
				dbCommandWrapper.AddInParameter("@UnitSettingID",  DbType.Int32, unitSettingID);
	            
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at DeleteUnitSetting method.");
			}
		}

		public virtual void DeleteUnitSettingByConPoint(int conPointID, int unitSettingID)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUnitSettingDeleteByConPoint");
	            
				dbCommandWrapper.AddInParameter("@ConPointID",  DbType.Int32, conPointID);
				dbCommandWrapper.AddInParameter("@UnitSettingID",  DbType.Int32, unitSettingID);
	            
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at DeleteUnitSettingByConPoint method.");
			}
		}

		public virtual void DeleteAllUnitSettingsByConPoint(int conPointID)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUnitSettingsDeleteByConPointIDOnly");
	            
				dbCommandWrapper.AddInParameter("@ConPointID",  DbType.Int32, conPointID);
	            
				DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at DeleteUnitSettingByConPoint method.");
			}
		}

		#endregion

		#region GetUnitSetting methods
    
		public virtual UnitSetting GetUnitSetting(int unitSettingID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUnitSettingGet");
            
				dbCommandWrapper.AddInParameter("@UnitSettingID", DbType.Int32, unitSettingID);
            
				UnitSetting unitSetting =  null;
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					if (reader.Read())
					{
						unitSetting = CreateUnitSettingFromReader(reader);
						reader.Close();
					}
				}
				return unitSetting;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at GetUnitSetting method.");
			}
		}
    
		#endregion

		#region GetUnitSettingList methods
    
		public virtual UnitSettingCollection GetUnitSettingList()
		{            
			try
			{
				UnitSettingCollection result = new UnitSettingCollection();
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("GetUnitSetting");
            
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						UnitSetting item = CreateUnitSettingFromReader(reader);
						result.Add(item);
					}
					reader.Close();
				}
				return result;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at GetUnitSetting method.");
			}
		}
    

		public virtual UnitSettingCollection GetUnitSettingsOnly()
		{            
			try
			{
				UnitSettingCollection result = new UnitSettingCollection();
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("GetUnitSettingsOnly");
            
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						UnitSetting item = CreateUnitSettingFromReader(reader);
						result.Add(item);
					}
					reader.Close();
				}
				return result;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("Error at GetUnitSettingsOnly method.");
			}
		}
    

		public virtual UnitSettingCollection GetUnitSettingsOnlyByConPoint(int conPointID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spUnitSettingGetByConPoint");
                
				dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, conPointID);
   
				UnitSettingCollection result = new UnitSettingCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						UnitSetting item = CreateUnitSettingFromReader(reader);
						if (!reader.IsDBNull(reader.GetOrdinal("IsInUse"))) item.IsInUse = Convert.ToBoolean(reader["IsInUse"]);
						result.Add(item);
					}
					reader.Close();
				}
				
				return result;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException("GetUnitSettingsOnlyByConPoint", ex);
			}
		}
		#endregion
		}
}